<?php

/**
 * This is the model class for table "es_cart".
 *
 * The followings are the available columns in table 'es_cart':
 * @property integer $cart_id
 * @property integer $user_id
 * @property integer $goods_id
 * @property string $price
 * @property integer $quantity
 * @property string $subtotal
 * @property integer $add_time
 * @property string $good_name
 */
class Cart extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Cart the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'es_cart';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('quantity', 'required'),
			array('user_id, goods_id, quantity, add_time', 'numerical', 'integerOnly'=>true),
			array('price, subtotal', 'length', 'max'=>10),
			array('good_name', 'length', 'max'=>255),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('cart_id, user_id, goods_id, price, quantity, subtotal, add_time, good_name', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'user'=>array(self::BELONGS_TO,'User','user_id'),
			'goods'=>array(self::MANY_MANY,'Goods','goods_id'),	
			);
	}
	
	/**
	 * The opration to done before insert into the database; 
	 */
	public function afterValidate()
	{
		$this->goods_id = $_GET['goods_id'];
		$this->user_id = Yii::app()->user->id;
		$good = Goods::model()->findByPk($this->goods_id);
		$this->price = $good->goods_price;
		$this->good_name = $good->goods_name;
		$this->subtotal = $good->goods_price*$this->quantity;
		$this->add_time = time();
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'cart_id' => 'Cart',
			'user_id' => 'User',
			'goods_id' => 'Good',
			'price' => 'Price',
			'quantity' => 'Quantity',
			'subtotal' => 'Subtotal',
			'add_time' => 'Add Time',
			'good_name' => 'Good Name',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('cart_id',$this->cart_id);
		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('goods_id',$this->goods_id);
		$criteria->compare('price',$this->price,true);
		$criteria->compare('quantity',$this->quantity);
		$criteria->compare('subtotal',$this->subtotal,true);
		$criteria->compare('add_time',$this->add_time);
		$criteria->compare('good_name',$this->good_name,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}